/*
 * @Author: liuying 247220246@qq.com
 * @Date: 2025-03-12 11:35:33
 * @LastEditors: liuying 247220246@qq.com
 * @LastEditTime: 2025-04-25 10:36:45
 * @FilePath: \my-book-react\src\stores\hooks.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { TypedUseSelectorHook, useDispatch, useSelector } from 'react-redux';
import { AppDispatch, AppState, store } from '@/stores/store';
import React from 'react';
// 导出一个名为useReducer的函数，该函数接受一个reducers参数，默认为空对象
export const useReducer = (reducers = {}) => {
  // 获取dispatch函数
  const dispatch = useDispatch<AppDispatch>();
  // 将reducers添加到store的reducerManager中
  store.reducerManager.addReducers(reducers);
  // 发送一个@RELOAD_STATE类型的action
  dispatch({ type: '@RELOAD_STATE' });
  // 使用React.useLayoutEffect函数，在组件卸载时移除reducers
  React.useLayoutEffect(() => {
    return () => {
      store.reducerManager.removeReducers(Object.keys(reducers));
    };
  }, [reducers]);
};
// 导出一个函数，用于获取AppDispatch类型的dispatch
export const useAppDispatch = () => useDispatch<AppDispatch>();
export const useAppSelector: TypedUseSelectorHook<AppState> = useSelector;
